package fon.rm2.client.wifi;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
//import android.util.Log;
import android.widget.Toast;

public class WiFiScanReceiver extends BroadcastReceiver 
{
	  //private static final String TAG = "WiFiScanReceiver";
	  //private StringBuffer strBuff = null;
	  private List<ScanResult> results = null;
	  private ScanResult bestSignal = null;
	  private WiFiActivity wifiActivity;
	
	  public WiFiScanReceiver(WiFiActivity wifiActivity) 
	  {
		  super();
		  this.wifiActivity = wifiActivity;
	  }
	
	  @Override
	  public void onReceive(Context c, Intent intent) 
	  {
		  try
		  {
			  results = wifiActivity.getWifi().getScanResults();
			  //this.strBuff = new StringBuffer();
			  bestSignal = null;

			  this.wifiActivity.showAvailableNetworks(results);
			
			  for (ScanResult result : results) 
			  {
				  if (bestSignal == null || WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
					  bestSignal = result;
				 // strBuff.append("\n\n" + result.SSID);
			  }
//			  
//			  strBuff.append("\n\n\nCurrently strongest signal: " + bestSignal.SSID);
//			  
//			  this.wifiActivity.showAvailableNetworks(strBuff);
//		
			  //String message = String.format("%s networks found. %s is the strongest.", results.size(), bestSignal.SSID);
			  //Toast.makeText(wifiActivity, message, Toast.LENGTH_LONG).show();
//		
//			  Log.d(TAG, "onReceive() message: " + message);
		  }
		  catch(Exception ex)
		  {
			  Toast.makeText(wifiActivity, ex.getMessage(), Toast.LENGTH_LONG).show();
		  }
	  }
}